package com.mrd.gtimpframe.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.gtimp.base.service.CompanyBiz;
import com.mrd.gtimpframe.entity.TGenSupSrvProvide;
import com.mrd.gtimpframe.service.TGenSupSrvProvideService;
import com.mrd.util.FormBeanUtil;
import com.mrd.util.JsonBinder;
import com.mrd.util.LoginUtil;
import com.mrd.util.R;
import com.mrd.util.ResponseUtils;

@Controller
@RequestMapping("supSrvProvide")
public class TGenSupSrvProvideAction {
  @Autowired
  private TGenSupSrvProvideService tGenSupSrvProvideService;
  @Autowired
  private CompanyBiz companyBiz;

  @RequestMapping("listPage")
  public String listPage() {
    return "/gtimpframe/supSrvProvide/supSrvProvide";
  }

  /**
   * 列表
   */
  @ResponseBody
  @RequestMapping("/pageList")
  public void pageList(Jqgrid jqgrid, HttpServletResponse response) throws Exception {
    String result = "";
    Pageable page = jqgrid.getPage();
    int pageNumber = page.getPageNumber();
    int pageSize = page.getPageSize();
    // 分页
    PageHelper.startPage(pageNumber + 1, pageSize);

    // 排序
    Sort.Order order = jqgrid.getOrder();
    if (order != null) {
      String direction = order.getDirection().toString();
      String propertyName = order.getProperty();
      PageHelper.orderBy(propertyName + " " + direction);
    }
    TGenSupSrvProvide tGenSupSrvProvide =
        FormBeanUtil.getEntityFromJqgrid(jqgrid, TGenSupSrvProvide.class);
    List<TGenSupSrvProvide> supSrvProvideList =
        tGenSupSrvProvideService.queryList(tGenSupSrvProvide);
    PageInfo<TGenSupSrvProvide> pi = new PageInfo<TGenSupSrvProvide>(supSrvProvideList);
    Page<Map<String, Object>> pMap = new PageImpl(supSrvProvideList, page, pi.getTotal());
    JsonBinder jsonBinder = JsonBinder.buildNormalBinder();
    jsonBinder.setDateFormat("yyyy-MM-dd HH:mm:ss");
    result = jsonBinder.toJson(pMap);
    ResponseUtils.renderJson(response, result);
  }

  @ResponseBody
  @RequestMapping("list")
  public List<TGenSupSrvProvide> list() {
    TGenSupSrvProvide tGenSupSrvProvide = new TGenSupSrvProvide();
    List<TGenSupSrvProvide> supSrvProvideList =
        tGenSupSrvProvideService.queryList(tGenSupSrvProvide);
    return supSrvProvideList;
  }

  @ResponseBody
  @RequestMapping("getCompanyList")
  public List<JSONObject> getCompanyList() throws Exception {
    JSONArray list = new JSONArray();
    // List<Company> listCompanys = companyBiz.findAllEntity();
    // if (listCompanys != null && listCompanys.size() > 0) {
    // for (Company company : listCompanys) {
    // if (StringUtils.isNotEmpty(company.getIsabled()) && company.getIsabled().equals("0")) {
    // JSONObject obj = new JSONObject();
    // obj.put("id", company.getId());
    // obj.put("name", company.getName());
    // list.add(obj);
    // }
    // }
    // }
    List<JSONObject> objs = tGenSupSrvProvideService.selectOffLineSup(null);
    return objs;
  }

  /**
   * 保存
   */
  @ResponseBody
  @RequestMapping("/save")
  public R save(@RequestBody TGenSupSrvProvide supSrvProvide) {
    supSrvProvide.setCreateTime(new Date());
    supSrvProvide.setCreator(LoginUtil.getLoginUser().getAccount());
    tGenSupSrvProvideService.insert(supSrvProvide);
    return R.ok();
  }

  /**
   * 修改
   */
  @ResponseBody
  @RequestMapping("/update")
  public R update(@RequestBody TGenSupSrvProvide supSrvProvide) {
    TGenSupSrvProvide rtnEntity =
        tGenSupSrvProvideService.selectByPrimaryKey(supSrvProvide.getId());
    supSrvProvide.setCreator(rtnEntity.getCreator());
    supSrvProvide.setCreateTime(rtnEntity.getCreateTime());
    supSrvProvide.setModifiyTime(new Date());
    supSrvProvide.setModifier(LoginUtil.getLoginUser().getAccount());
    tGenSupSrvProvideService.updateByPrimaryKey(supSrvProvide);
    return R.ok();
  }

  /**
   * 信息
   */
  @ResponseBody
  @RequestMapping("/info/{id}")
  public R info(@PathVariable("id") String id) {
    TGenSupSrvProvide supSrvProvide = tGenSupSrvProvideService.selectByPrimaryKey(id);
    return R.ok().put("supSrvProvide", supSrvProvide);
  }

  /**
   * 删除
   */
  @ResponseBody
  @RequestMapping("/delete")
  public R delete(@RequestBody String[] ids) {
    tGenSupSrvProvideService.deleteBatch(ids);
    return R.ok();
  }

  /**
   * 信息
   */
  @ResponseBody
  @RequestMapping("/selectOffLineSup")
  public List<JSONObject> selectOffLineSup(String code) {
    List<JSONObject> list = tGenSupSrvProvideService.selectOffLineSup(code);
    return list;
  }
}
